package com.lcz.web;

import com.lcz.service.StudentService;
import com.lcz.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/testuno")
public class TestStunameServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数
        String stuno = req.getParameter("stuno");

        //2.调取Service，获得所有的用户名
        StudentService studentService = new StudentServiceImpl();
        List list = studentService.isStuname();

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter printWriter = resp.getWriter();

        //3.判断学号是否在数据库中。
        for(int i =0;i<list.size();i++){
            if(list.get(i).equals(stuno)){
                printWriter.println("账户已注册");
                System.out.println("学号"+list.get(i));
                return;
            }
        }
        printWriter.println("账户可用");
    }
}
